package com.zrsx.searchmodel.service.serviceImpl;

import com.zrsx.searchmodel.common.constants.TypeConstants;
import com.zrsx.searchmodel.common.vo.CountVO;
import com.zrsx.searchmodel.mapper.RecordMapper;
import com.zrsx.searchmodel.mapper.UserMapper;
import com.zrsx.searchmodel.service.CountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Service
@Slf4j
public class CountServiceImpl implements CountService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public CountVO getCount(Integer userId) {
        CountVO countVO = new CountVO();
        //1.累计天数
        LocalDateTime registerTime = userMapper.selectRegisterTimeById(userId);
        LocalDateTime now = LocalDateTime.now();
        // 累计天数这里为直接计算从注册日期到现在的天数，无论是否登录（包扩了当天）
        long days = ChronoUnit.DAYS.between(registerTime.toLocalDate(), now.toLocalDate()) + 1;
        countVO.setTotalDays(days);
        //2.累计金额
        Double totalAmount = recordMapper.selectTotalAmount(userId);
        if (totalAmount == null) totalAmount = 0.0;
        countVO.setTotalAmount(totalAmount);
        //3.信用生活使用次数
        Integer lifeCount = recordMapper.selectUseCount(userId, TypeConstants.CreditLife);
        countVO.setCreditLifeCount(lifeCount);
        //4.信用商业使用次数
        Integer businessCount = recordMapper.selectUseCount(userId, TypeConstants.CreditBusiness);
        countVO.setCreditBusinessCount(businessCount);
        log.info("获取到信用行为统计信息：" + countVO);
        return countVO;
    }
}
